package com.easyweb.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.easyweb.cache.StartUpListener;
import com.easyweb.common.Constants;
import com.easyweb.common.EncryptUtils;
import com.easyweb.common.PrimaryKeyGenerator;
import com.easyweb.common.PrintUtils;
import com.easyweb.common.ValidateCode;
import com.easyweb.model.ResultMsg;
import com.easyweb.model.Users;
import com.easyweb.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/register",method=RequestMethod.POST)
	public void add(HttpServletRequest request,HttpServletResponse response){
		String userName = request.getParameter("uname");
		if(StringUtils.isEmpty(userName)){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("username.notnull")));
			return;
		}
		if(userName.length()>50){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("username.toolarge")));
			return;
		}
		String password = request.getParameter("pwd");
		if(StringUtils.isEmpty(password)){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("password.notnull")));
			return;
		}
		if(password.length()>20){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("password.toolarge")));
			return;
		}
		//check username
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userName", userName);
		List<Users> users = userService.getUsers(map);
		if(users!=null&&users.size()>0){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("username.hassame")));
			return;
		}
		Users u = new Users();
		u.setId(PrimaryKeyGenerator.generatePrimaryKey());
		u.setUserName(userName);
		u.setPwd(EncryptUtils.SHA(password));
		int count = userService.insertUsers(u);
		if(count>0){
			PrintUtils.printMsg(response, new ResultMsg());
		}else{
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("error.unkonw")));
		}
	}
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public void login(HttpServletRequest request,HttpServletResponse response){
		String userName = request.getParameter("uname");
		if(StringUtils.isEmpty(userName)){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("username.notnull")));
			return;
		}
		String password = request.getParameter("pwd");
		if(StringUtils.isEmpty(password)){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("password.notnull")));
			return;
		}
		String imgCode = request.getParameter("imgcode");
		if(StringUtils.isEmpty(imgCode)){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("validatecode.notnull")));
			return;
		}
		if(!imgCode.equalsIgnoreCase(request.getSession().getAttribute("code").toString())){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("validatecode.error")));
			return;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userName", userName);
		map.put("pwd", EncryptUtils.SHA(password));
		List<Users> users = userService.getUsers(map);
		if(users==null||users.size() == 0){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("nameorpwd.error")));
			return;
		}
		if(users.size() > 1){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("error.unkonw")));
			return;
		}
		request.getSession().setAttribute("user", users.get(0));
		PrintUtils.printMsg(response, new ResultMsg());
	}
	
	@RequestMapping(value="/imgvalidate",method=RequestMethod.GET)
	public void validateImage(HttpServletRequest request,HttpServletResponse response){
		ValidateCode code = new ValidateCode();
		response.setContentType("image/*");
		BufferedImage bufferedImage = code.getBufferedImage();
		request.getSession().setAttribute("code", code.getText());
		try {
			ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value="/logout",method=RequestMethod.GET)
	public void logout(HttpServletRequest request,HttpServletResponse response){
		request.getSession().removeAttribute("user");
		PrintUtils.printMsg(response, new ResultMsg());
	}
}
